<?php
namespace app\admin\controller;

use app\common\model\data\Language;
use app\admin\model\WechatUserModel;
use app\common\model\WechatAppletModel;
use app\yxwadmin\controller\Admin;
use think\Exception;
use think\Request;

class User extends Admin
{

    public $error_msg = '';

    /**
     * 首页
     * @return string
     * @throws \think\Exception
     */
    public function index()
    {
        // 小程序
        $appletM = new WechatAppletModel();
        $appletD = $appletM->field('id, name')->where(['product_code' => 'photo_albums_bless'])->select();
        $this->assign('applet_list', $appletD);
        return $this->view->fetch();
    }

    /**
     * 初始化表格
     * 关键字搜索
     * 分页
     * @throws
     * @return mixed
     */
    public function getList()
    {
        $limit = input('limit','0');
        $offset = input('offset','0');
        $sortName = input('sortName','id');
        $sortOrder = input('sortOrder','desc');
        $keywords = input('keywords','');
        $applet_id = input('applet_id', '');
        if ($applet_id) {
            $where['applet_id'] = $applet_id;
        } else {
            $where = '1=1';
        }

        $model = new WechatUserModel();
        if ($keywords) {
            $count = $model->where($where)->where('country|province|city|nick_name|openid|language|original_id|applet_name', 'like', '%' . $keywords . '%')->count();
            $list = $model
                ->where($where)
                ->where('country|province|city|nick_name|openid|language|original_id|applet_name', 'like', '%' . $keywords . '%')
                ->order([$sortName => $sortOrder])
                ->limit($offset . ',' . $limit)
                ->select();
        } else {
            $count = $model->where($where)->count(); //查询满足要求的总记录数
            $list = $model
                ->where($where)
                ->order(array($sortName=>$sortOrder))
                ->limit($offset.','.$limit)
                ->select();
        }

        $data['total'] = $count;
        $data['rows'] = $list;
        return $data;
    }

    /*public function add() {
        if (request()->isPost()) {
            $data = $_REQUEST;
            $validate = new \app\admin\model\validate\WechatUser();
            $res = $validate->check($data);
            if (!$res) {
                $this->error_msg = $validate->getError();
                return json(['code' => 0, 'msg' => $this->error_msg, 'data' => null]);
            }

            $model = new WechatUserModel($data);
            $model->created_time = date('Y-m-d H:i:s', time());
            $model->updated_time = date('Y-m-d H:i:s', time());
            $res = $model->allowField(true)->save();
            if (!$res) {
                $this->error_msg = $model->getError();
                return json(['code' => 0, 'msg' => $this->error_msg, 'data' => null]);
            }

            return json(['code' => 1, 'msg' => '新增成功', 'data' => null]);

        } else {
            // 小程序
            $appletM = new WechatAppletModel();
            $appletD = $appletM->field('id, name, applet_appid, original_id, app_id')->where(['product_code' => 'jk_hongbao'])->select();
            $this->assign('applet_list', $appletD);

            $this->assign('category', ImageCategory::CATEGORY_AVATAR);
            $this->assign('language_list', Language::languageArray());
            $this->assign('status_list', WechatUserModel::statusArray());
            $this->assign('type_list', WechatUserModel::typeArray());
            $this->assign('gender_list', WechatUserModel::genderArray());
            return $this->fetch();
        }
    }

    public function edit() {
        $id = $_REQUEST['id'];
        $model = new WechatUserModel();

        if (request()->isPost()) {
            $data = $_REQUEST;
            $validate = new \app\admin\model\validate\WechatUser();
            $res = $validate->check($data);
            if (!$res) {
                $this->error_msg = $validate->getError();
                return json(['code' => 0, 'msg' => $this->error_msg, 'data' => null]);
            }

            $data['updated_time'] = date('Y-m-d H:i:s', time());
            $res = $model->allowField(true)->where(['id' => $id])->update($data);

            if (!$res) {
                $this->error_msg = $model->getError();
                return json(['code' => 0, 'msg' => $this->error_msg, 'data' => null]);
            }
            return json(['code' => 1, 'msg' => '编辑成功', 'data' => null]);

        } else {
            // 小程序
            $appletM = new WechatAppletModel();
            $appletD = $appletM->field('id, name, applet_appid, original_id, app_id')->where(['product_code' => 'jk_hongbao'])->select();
            $this->assign('applet_list', $appletD);

            $this->assign('category', ImageCategory::CATEGORY_AVATAR);
            $this->assign('language_list', Language::languageArray());
            $this->assign('status_list', WechatUserModel::statusArray());
            $this->assign('type_list', WechatUserModel::typeArray());
            $this->assign('gender_list', WechatUserModel::genderArray());
            $Rs = $model->where(['id' => $id])->find();
            $this->assign('Rs', $Rs);
            return $this->fetch();
        }
    }*/

    public function view() {
        $id = input('id/d');
        $model = new WechatUserModel();
        $Rs = $model->where(['id' => $id])->find();
        $this->assign('Rs', $Rs);
        return $this->fetch();
    }

    /**
     * 加入系统用户
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function system()
    {
        $id = input('id/d');
        $model = new WechatUserModel();
        $where = ['id'=>$id];
        $data = $model->where($where)->find();
        if (empty($data)) {
            return json(['code' => 0, 'msg' => '没有该用户数据']);
        }
        $status = WechatUserModel::IS_SYSTEM_NO;
        if ($data['is_system'] == WechatUserModel::IS_SYSTEM_NO) {
            $status = WechatUserModel::IS_SYSTEM_YES;
        }
        $res = $model->save(['is_system' => $status], $where);
        if ($res) {
            $ret['code']=1;
            $ret['msg']='操作成功';
        } else {
            $ret['code']=0;
            $ret['msg']='服务器繁忙';
        }
        return json($ret);
    }

}